I have recently considered a lot of metaprogramming (metaprogramming) issues and would like to see more examples and explanations of this technology. For better or worse, Metaprogramming has entered the Ruby community and is a standard way to accomplish various tasks and simplify code. Since I can't find this kind of r
Basic Learning notes for Ruby metaprogramming and ruby learning notes
Note 1:The Code contains variables, classes, and methods, collectively referred to as language construct ).
# test.rbclass Greeting def initialize(text) @text = text end def welcome @text endendmy_obj = Greeting.new("hello")puts my_obj.classputs my_obj.class.instance_methods(false) #false mea
Ruby metaprogramming and ruby Programming
An object model
Kernel Module
Kernel. private_instance_methods.grep (/^ pr /)
Private method
1. If a method receiver is not yourself, you must specify a receiver.
2. A private method can only specify one implicit receiver (a private method that can be called as a superclass)
The Class itself is the object of the
Opening
Empty is the color, and the color is empty.Empty and empty, in the Ruby language, everything is an object.
Ruby is an object-oriented language (object oriented Language), and the object-oriented concept is much more robust than other languages.
The raw type data and object type data in Java are not present in Ruby. Most of the stuff in
Metaprogramming is a program that can operate other programs. for example, macros in C language are an advanced technique in programming. here we will take a look at the basics of Ruby Metaprogramming.
Note 1:The code contains variables, classes, and methods, collectively referred to as language construct ).
# test.rbclass Greeting def initialize(text) @text
Ruby metaprogramming is worth noting.
Avoid infinite loop metaprogramming.
Do not confuse the core class (do not use monkey patch) when writing a function library ).
It is best to use the code block form in the string interpolation form.When you use string interpolation, _ FILE _ and _ LINE __are always provided to make your backtracking meaningful.
class_eval '
Metaprogramming is a ruby feature that can dynamically modify the language structure, such as the class structure, module structure, and instance variable information.
You can evenProgramAdd and run the newCodeYou do not need to restart your program. 1. This document describes how to use attr_accessor, attr_reader, and attr_writer.
Class person # can contain multiple parameters to automatically generate
An object consists of a group of instance variables and a class reference.
Object methods exist in the class to which the object belongs (from the class perspective, they are called instance methods ).
The class itself is the object of the class. The class name is just a constant.
A class is a subclass of a module. A module is basically a package composed of a group of methods. In addition to the features of modules, classes can also be instantiated (through the new () method) and
Metaprogrammingisthewritingofcomputerprogramsthatwriteormanipulateotherprograms (orthemselves) astheirdata, orthatdopartoftheworkatcompiletimethatwouldotherwisebedonerecognition
Metaprogramming
Is the writing of computer programs
That write or manipulate other programs (or themselves) as their data,
OrThat do part of the work at compile time
That wocould otherwise be done at runtime
. In some cases, this allows programmers to minimize the numb
language likeRuby or groovy, Required implies capabilities that extend existing object models, add hooks to alter the behaviors of existing methods (or even classes), and synthesize new methods, properties, or modules during runtime through introspection.
Ages likeLISPUse macros as the metaprogramming tool that let you syntactically extend the language during the compilation stage. although the primary form of m
When using JavaScript to create DOM nodes, document. createelement, setattribute,Document. createtextnode and Other lengthy node operation methods with browser compatibility issues, although some people propose to use String concatenation beforeUsing. innerhtml = can reduce the cost of creating nodes and provide better performance. However, in my impression, innerhtml cannot meet all the requirements (I forgot the specific requirements ).The following describes how to dynamically create a node u
Document directory
Code vs. Data
Homoiconicity, code = Data
Working with macros
Debugging macros
Code Templating
When to use macros
Using macros, for example
What is metaprogramming?
Metaprogramming is the use of code to modify or create other code.It is primarily a developer tool and acts as a force multiplier, allowing large amounts of predictable code to be generated from just a few statements i
Python implements struct class instances using the decorator and metaprogramming. python instances
There is a convenient Struct class in Ruby to implement Struct. In this way, you do not have to define a complete class as an access attribute.Copy codeThe Code is as follows:Class Dog End
Fred = Dog. new ("fred", 5)Printf "name: % s age: % d", fred. name, fred. age# Name: fred age: 5This can also be done in P
Reading Notes Objective c ++ Item 48 learn about template metaprogramming and objective tiveitem1. What is TMP?
Template metaprogramming TMP) Is to implement template-Based C ++Program process, which can be executed during compilation. You can think about it: a template metaprogram is implemented in C ++ and can run in the C ++ compiler, its output-the C ++ source code snippet instantiated from the template
There are various containers in STL, And the STL algorithm allows us to perform various operations on the elements in the container. The following program should be easy for every contemporary c ++ programmer:
# Include # Include # Include # Include
Using namespace STD;
Struct print{Void operator () (const string _ Str){Cout }};
Int main (){List Str_list.push_front ("hello ");Str_list.push_front ("world ");List Another_list.push_back ("hello ");Another_list.push_back ("world ");For_each (str_
The idea of metaprogramming
When I read the previous functional programming section, I saw another idea of programming, called the metaprogramming.I think this word is very interesting. At the beginning, I thought it was the author's mistake. It should be the "Source" programming idea. After reading it carefully, the content is as follows:According to the definition of the free encyclopedia (http://www.wikipedia.org): write some Program To gene
I wanted to read Chinese after the Chinese translations of Hou Jie and glory, so I can feel at ease with the quality of the translations of the two teachers. Unfortunately wait for a long time, delay does not see out, in the china-pub.com many readers have been waiting impatiently. So did I, so it was a bit difficult to read the e-text electronic version of this book from the end of last November. After nearly two months of hard work, I finally read it for the first time, opening my eyes. Such a
that he could use the C ++ template for metaprogramming and published a technical report. In May of the following year, I published an article named "using C ++ template metaprograms" in C ++ report, in this way, the C ++ compiler template programming (compile-time) discovered by Erwin UnruhTemplate programming) further refined to C ++ template meta programming (template metaprogramming, TMP ).Thoughts:
Th
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.